﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "PositionDetail" here, you must also update the reference to "PositionDetail" in App.config.
    public partial class MainService : IPositionDetail
    {
        public DataSet PositionDetail_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetailn_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet PositionDetail_GetNewSalary()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetNewSalary", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet PositionDetail_GetOlderSalary()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetOlderSalary", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet PositionDetail_GetList(out int ItemCount, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        
        public DataSet PositionDetail_GetForInsurance(DateTime _DateVal, int BranchID, int DepartmentID, string _KeyWord)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetForInsurance", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@DateVal", _DateVal);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            dbCmd.Parameters.AddWithValue("@KeyWord", _KeyWord);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        
        public DataSet PositionDetail_GetWithEmployee(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@PD_EmployeeID", EmployeeID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet PositionDetail_BaoHiem(int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            //SqlCommand dbCmd = new SqlCommand("HRM_BaoHiem", dbConn);
            SqlCommand dbCmd = new SqlCommand("HRM_BaoHiem_GetForForm03", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet PositionDetail_DieuChinh(int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_DieuChinh", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet PositionDetail_GetMaxID(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetMaxID", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@PD_EmployeeID", EmployeeID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet PositionDetail_GetWithDepartment(int DepartmentID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetWithDepartment", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@PD_DepartmentID", DepartmentID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet PositionDetail_GetEmployeeLater(int DepartmentID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetEmployeeLater", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@PD_DepartmentID", DepartmentID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet PositionDetail_GetBranchWithEmployee(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetBranchWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public PositionDetailInfo PositionDetail_GetInfo(int EmployeeID)
        {
            PositionDetailInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Employee_ID", EmployeeID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new PositionDetailInfo();
                    retVal.ID = Convert.ToInt32(dr["PD_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["PD_EmployeeID"]);
                    retVal.DepartmentID = Globals.DB_GetValue<int>(dr["PD_DepartmentID"],0);
                    retVal.PositionID = Globals.DB_GetValue<int>(dr["PD_PositionID"],0);
                    retVal.GradeID = Globals.DB_GetValue<int>(dr["PD_GradeID"],0);
                    retVal.BeginDate = dr["PD_BeginDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_BeginDate"]);
                    retVal.EndDate = dr["PD_EndDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_EndDate"]);
                    retVal.IsActive = Globals.DB_GetValue<bool>(dr["PD_IsActive"]);
                    retVal.Description = Globals.DB_GetValue<string>(dr["PD_Description"]);
                    retVal.Job = Globals.DB_GetValue<string>(dr["PD_Job"].ToString());
                    retVal.LevelID = dr["PD_LevelID"] == DBNull.Value ? 0 : Convert.ToInt32(dr["PD_LevelID"]);
                    retVal.MinSalary = dr["PD_MinSalary"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_MinSalary"]);
                    retVal.LaterInsurance = dr["PD_LaterInsurance"] == DBNull.Value ? false : Convert.ToBoolean(dr["PD_LaterInsurance"]);
                    retVal.DatePositionBegin = dr["PD_DatePositionBegin"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_DatePositionBegin"]);
                    retVal.DatePositionEnd = dr["PD_DatePositionEnd"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_DatePositionEnd"]);
                    retVal.DateSalary = dr["PD_DateSalary"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_DateSalary"]);

                    //
                    retVal.BasicFactor = dr["PD_BasicFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_BasicFactor"]);
                    retVal.BasicSalary = dr["PD_BasicSalary"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_BasicSalary"]);
                    retVal.PositionFactor = dr["PD_PositionFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_PositionFactor"]);
                    retVal.PositionAllowance = dr["PD_PositionAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_PositionAllowance"]);
                    retVal.OverFactor = dr["PD_OverFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OverFactor"]);
                    retVal.OverAllowance = dr["PD_OverAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OverAllowance"]);
                    retVal.JobFactor = dr["PD_JobFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_JobFactor"]);
                    retVal.JobAllowance = dr["PD_JobAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_JobAllowance"]);
                    retVal.AreaFactor = dr["PD_AreaFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_AreaFactor"]);
                    retVal.AreaAllowance = dr["PD_AreaAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_AreaAllowance"]);
                    //
                    retVal.TelephoneAllowance = dr["PD_TelephoneAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_TelephoneAllowance"]);
                    retVal.LunchAllowance = dr["PD_LunchAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_LunchAllowance"]);
                    retVal.OtherFactor = dr["PD_OtherFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OtherFactor"]);
                    retVal.OtherAllowance = dr["PD_OtherAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OtherAllowance"]);
                    retVal.TotalSalary = Globals.DB_GetValue<decimal>(dr["PD_TotalSalary"],0);
                    //
                    
                    
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        public PositionDetailInfo PositionDetail_GetInfoMaxDate(int EmployeeID)
        {
            PositionDetailInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetInfoMaxDate", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Employee_ID", EmployeeID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new PositionDetailInfo();
                    retVal.ID = Convert.ToInt32(dr["PD_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["PD_EmployeeID"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["PD_DepartmentID"]);
                    retVal.PositionID = Convert.ToInt32(dr["PD_PositionID"]);
                    retVal.GradeID = Convert.ToInt32(dr["PD_GradeID"]);
                    retVal.BeginDate = dr["PD_BeginDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_BeginDate"]);
                    retVal.EndDate = dr["PD_EndDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_EndDate"]);
                    retVal.IsActive = Convert.ToBoolean(dr["PD_IsActive"]);
                    retVal.Description = Convert.ToString(dr["PD_Description"]);
                    retVal.Job = dr["PD_Job"].ToString();
                    retVal.LevelID = dr["PD_LevelID"] == DBNull.Value ? 0 : Convert.ToInt32(dr["PD_LevelID"]);
                    retVal.MinSalary = dr["PD_MinSalary"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_MinSalary"]);
                    retVal.LaterInsurance = dr["PD_LaterInsurance"] == DBNull.Value ? false : Convert.ToBoolean(dr["PD_LaterInsurance"]);
                    retVal.DatePositionBegin = dr["PD_DatePositionBegin"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_DatePositionBegin"]);
                    retVal.DatePositionEnd = dr["PD_DatePositionEnd"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_DatePositionEnd"]);
                    retVal.DateSalary = dr["PD_DateSalary"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_DateSalary"]);

                    //
                    retVal.BasicFactor = dr["PD_BasicFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_BasicFactor"]);
                    retVal.BasicSalary = dr["PD_BasicSalary"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_BasicSalary"]);
                    retVal.PositionFactor = dr["PD_PositionFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_PositionFactor"]);
                    retVal.PositionAllowance = dr["PD_PositionAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_PositionAllowance"]);
                    retVal.OverFactor = dr["PD_OverFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OverFactor"]);
                    retVal.OverAllowance = dr["PD_OverAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OverAllowance"]);
                    retVal.JobFactor = dr["PD_JobFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_JobFactor"]);
                    retVal.JobAllowance = dr["PD_JobAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_JobAllowance"]);
                    retVal.AreaFactor = dr["PD_AreaFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_AreaFactor"]);
                    retVal.AreaAllowance = dr["PD_AreaAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_AreaAllowance"]);
                    //
                    retVal.TelephoneAllowance = dr["PD_TelephoneAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_TelephoneAllowance"]);
                    retVal.LunchAllowance = dr["PD_LunchAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_LunchAllowance"]);
                    retVal.OtherFactor = dr["PD_OtherFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OtherFactor"]);
                    retVal.OtherAllowance = dr["PD_OtherAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OtherAllowance"]);
                    retVal.TotalSalary = Convert.ToDecimal(dr["PD_TotalSalary"]);
                    //
                    
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public PositionDetailInfo PositionDetail_GetInfoWithPDID(int PDID)
        {
            PositionDetailInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_GetInfoWithPDID", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@PD_ID", PDID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new PositionDetailInfo();
                    retVal.ID = Convert.ToInt32(dr["PD_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["PD_EmployeeID"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["PD_DepartmentID"]);
                    retVal.PositionID = Convert.ToInt32(dr["PD_PositionID"]);
                    retVal.GradeID = Convert.ToInt32(dr["PD_GradeID"]);
                    retVal.BeginDate = dr["PD_BeginDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_BeginDate"]);
                    retVal.EndDate = dr["PD_EndDate"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_EndDate"]);
                    retVal.IsActive = Convert.ToBoolean(dr["PD_IsActive"]);
                    retVal.Description = Convert.ToString(dr["PD_Description"]);
                    retVal.Job = dr["PD_Job"].ToString();
                    retVal.LevelID = dr["PD_LevelID"] == DBNull.Value ? 0 : Convert.ToInt32(dr["PD_LevelID"]);
                    retVal.MinSalary = dr["PD_MinSalary"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_MinSalary"]);
                    retVal.LaterInsurance = dr["PD_LaterInsurance"] == DBNull.Value ? false : Convert.ToBoolean(dr["PD_LaterInsurance"]);
                    retVal.DatePositionBegin = dr["PD_DatePositionBegin"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_DatePositionBegin"]);
                    retVal.DatePositionEnd = dr["PD_DatePositionEnd"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_DatePositionEnd"]);
                    retVal.DateSalary = dr["PD_DateSalary"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["PD_DateSalary"]);

                    //
                    retVal.BasicFactor = dr["PD_BasicFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_BasicFactor"]);
                    retVal.BasicSalary = dr["PD_BasicSalary"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_BasicSalary"]);
                    retVal.PositionFactor = dr["PD_PositionFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_PositionFactor"]);
                    retVal.PositionAllowance = dr["PD_PositionAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_PositionAllowance"]);
                    retVal.OverFactor = dr["PD_OverFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OverFactor"]);
                    retVal.OverAllowance = dr["PD_OverAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OverAllowance"]);
                    retVal.JobFactor = dr["PD_JobFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_JobFactor"]);
                    retVal.JobAllowance = dr["PD_JobAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_JobAllowance"]);
                    retVal.AreaFactor = dr["PD_AreaFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_AreaFactor"]);
                    retVal.AreaAllowance = dr["PD_AreaAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_AreaAllowance"]);
                    //
                    retVal.TelephoneAllowance = dr["PD_TelephoneAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_TelephoneAllowance"]);
                    retVal.LunchAllowance = dr["PD_LunchAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_LunchAllowance"]);
                    retVal.OtherFactor = dr["PD_OtherFactor"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OtherFactor"]);
                    retVal.OtherAllowance = dr["PD_OtherAllowance"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["PD_OtherAllowance"]);
                    retVal.TotalSalary = Convert.ToDecimal(dr["PD_TotalSalary"]);
                    //
                    
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int PositionDetail_Insert(PositionDetailInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            if (eInfo.BeginDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@PD_BeginDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@PD_BeginDate", eInfo.BeginDate);
            dbCmd.Parameters.AddWithValue("@PD_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@PD_Description", eInfo.Description);
            dbCmd.Parameters.AddWithValue("@PD_EmployeeID", eInfo.EmployeeID);
            if (eInfo.EndDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@PD_EndDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@PD_EndDate", eInfo.EndDate);
            dbCmd.Parameters.AddWithValue("@PD_GradeID", eInfo.GradeID);
            dbCmd.Parameters.AddWithValue("@PD_IsActive", eInfo.IsActive);
            dbCmd.Parameters.AddWithValue("@PD_PositionID", eInfo.PositionID);
            dbCmd.Parameters.AddWithValue("@PD_BasicSalary", eInfo.BasicSalary);
            dbCmd.Parameters.AddWithValue("@PD_PositionAllowance", eInfo.PositionAllowance);
            dbCmd.Parameters.AddWithValue("@PD_TotalSalary", eInfo.TotalSalary);

            dbCmd.Parameters.AddWithValue("@PD_LevelID", eInfo.LevelID);
            dbCmd.Parameters.AddWithValue("@PD_MinSalary", eInfo.MinSalary);
            dbCmd.Parameters.AddWithValue("@PD_BasicFactor", eInfo.BasicFactor);
            dbCmd.Parameters.AddWithValue("@PD_PositionFactor", eInfo.PositionFactor);
            dbCmd.Parameters.AddWithValue("@PD_OverFactor", eInfo.OverFactor);

            dbCmd.Parameters.AddWithValue("@PD_OverAllowance", eInfo.OverAllowance);

            dbCmd.Parameters.AddWithValue("@PD_JobFactor", eInfo.JobFactor);
            dbCmd.Parameters.AddWithValue("@PD_JobAllowance", eInfo.JobAllowance);

            dbCmd.Parameters.AddWithValue("@PD_AreaFactor", eInfo.AreaFactor);
            dbCmd.Parameters.AddWithValue("@PD_AreaAllowance", eInfo.AreaAllowance);

            dbCmd.Parameters.AddWithValue("@PD_OtherFactor", eInfo.OtherFactor);
            dbCmd.Parameters.AddWithValue("@PD_OtherAllowance", eInfo.PositionAllowance);
            dbCmd.Parameters.AddWithValue("@PD_LunchAllowance", eInfo.LunchAllowance);
            dbCmd.Parameters.AddWithValue("@PD_TelephoneAllowance", eInfo.TelephoneAllowance);
            dbCmd.Parameters.AddWithValue("@PD_LaterInsurance", eInfo.LaterInsurance);
            dbCmd.Parameters.AddWithValue("@PD_Job", eInfo.Job);
            if (eInfo.DatePositionBegin == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@PD_DatePositionBegin", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@PD_DatePositionBegin", eInfo.DatePositionBegin);
            if (eInfo.DatePositionEnd == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@PD_DatePositionEnd", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@PD_DatePositionEnd", eInfo.DatePositionEnd);
            if (eInfo.DateSalary == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@PD_DateSalary", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@PD_DateSalary", eInfo.DateSalary);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool PositionDetail_Update(PositionDetailInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@PD_ID", eInfo.ID);
            if (eInfo.BeginDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@PD_BeginDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@PD_BeginDate", eInfo.BeginDate);
            dbCmd.Parameters.AddWithValue("@PD_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@PD_Description", eInfo.Description);
            dbCmd.Parameters.AddWithValue("@PD_EmployeeID", eInfo.EmployeeID);
            if (eInfo.EndDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@PD_EndDate", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@PD_EndDate", eInfo.EndDate);
            dbCmd.Parameters.AddWithValue("@PD_GradeID", eInfo.GradeID);
            dbCmd.Parameters.AddWithValue("@PD_IsActive", eInfo.IsActive);
            dbCmd.Parameters.AddWithValue("@PD_PositionID", eInfo.PositionID);

            dbCmd.Parameters.AddWithValue("@PD_BasicSalary", eInfo.BasicSalary);
            dbCmd.Parameters.AddWithValue("@PD_PositionAllowance", eInfo.PositionAllowance);

            dbCmd.Parameters.AddWithValue("@PD_TotalSalary", eInfo.TotalSalary);
            dbCmd.Parameters.AddWithValue("@PD_LevelID", eInfo.LevelID);
            dbCmd.Parameters.AddWithValue("@PD_MinSalary", eInfo.MinSalary);
            dbCmd.Parameters.AddWithValue("@PD_BasicFactor", eInfo.BasicFactor);
            dbCmd.Parameters.AddWithValue("@PD_PositionFactor", eInfo.PositionFactor);
            dbCmd.Parameters.AddWithValue("@PD_OverFactor", eInfo.OverFactor);
            dbCmd.Parameters.AddWithValue("@PD_OverAllowance", eInfo.OverAllowance);

            dbCmd.Parameters.AddWithValue("@PD_JobFactor", eInfo.JobFactor);
            dbCmd.Parameters.AddWithValue("@PD_JobAllowance", eInfo.JobAllowance);

            dbCmd.Parameters.AddWithValue("@PD_AreaFactor", eInfo.AreaFactor);
            dbCmd.Parameters.AddWithValue("@PD_AreaAllowance", eInfo.AreaAllowance);
            dbCmd.Parameters.AddWithValue("@PD_OtherFactor", eInfo.OtherFactor);
            dbCmd.Parameters.AddWithValue("@PD_OtherAllowance", eInfo.OtherAllowance);
            dbCmd.Parameters.AddWithValue("@PD_LunchAllowance", eInfo.LunchAllowance);
            dbCmd.Parameters.AddWithValue("@PD_TelephoneAllowance", eInfo.TelephoneAllowance);
            dbCmd.Parameters.AddWithValue("@PD_LaterInsurance", eInfo.LaterInsurance);
            dbCmd.Parameters.AddWithValue("@PD_Job", eInfo.Job);
            if (eInfo.DatePositionBegin == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@PD_DatePositionBegin", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@PD_DatePositionBegin", eInfo.DatePositionBegin);
            if (eInfo.DatePositionEnd == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@PD_DatePositionEnd", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@PD_DatePositionEnd", eInfo.DatePositionEnd);
            if (eInfo.DateSalary == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@PD_DateSalary", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@PD_DateSalary", eInfo.DateSalary);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool PositionDetail_UpdateEmployee(int EmployeeID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_UpdateEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool PositionDetail_UpdateStatusEmployee(int EmployeeID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_UpdateStatusEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool PositionDetail_ActiveEmployee(int PDID, DateTime date)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_ActiveEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@PDID", PDID);
            dbCmd.Parameters.AddWithValue("@date", date);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool PositionDetail_UpdateEmployeeLater(int EmployeeID, DateTime date)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_UpdateEmployeeLater", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@date", date);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool PositionDetail_Delete(int PositionDetailID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@PD_ID", PositionDetailID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool PositionDetail_CountEmployeeWithDept(int DepartmentID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_PositionDetail_CountEmployeeWithDept", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            try
            {
                dbConn.Open();
                int Count = Convert.ToInt32(dbCmd.ExecuteScalar());
                retVal = Count > 0 ? true : false;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}




